﻿
using System.Collections.Generic;
using System.Data;
using System;
using System.Linq;

namespace ZHDJ.MobileInterface
{
    public partial class GetMyExaminTopicExList : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            //var specialId = ServiceSettings.TestSpecialIds.Split(';');
            var specialId = InterfaceHelper.GetSpecialID().Keys.ToArray();
            MyExaminTopicExOutParam result = new MyExaminTopicExOutParam();
            MyExaminTopicExInParam param = new MyExaminTopicExInParam();
            string logMsg = "";
            bool isFlag = true;
                 
            try
            {
                param = inParam as MyExaminTopicExInParam;

                if (string.IsNullOrEmpty(param.special_id))
                {
                    result.code = "0";
                    result.desc = "专题ID不能为空";
                    isFlag = false;
                    //param.special_id = "qzlxjysj";
                }

                if (isFlag)
                {
                    if (!specialId.Where(a => a == param.special_id).Any())
                    {
                        result.code = "0";
                        result.desc = "专题ID不正确";
                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    if (string.IsNullOrEmpty(param.user_id))
                    {
                        result.code = "0";
                        result.desc = "用户ID不能为空";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    if (CommonUtils.IsDisableUsers(param.user_id))
                    {
                        result.code = "0";
                        result.desc = "用户状态有问题，请联系管理员";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    //用户是否存在
                    isFlag = InterfaceHelper.CheckLoginIDCountEx(param.user_id);

                    if (!isFlag)
                    {
                        result.code = "0";
                        result.desc = "该用户不存在";
                    }
                }   

                if(isFlag)
                {
                    DataSet ds = InterfaceHelper.GetExamin(param.user_id, param.special_id);
                    if (ds != null && ds.Tables[0].Rows.Count >= 3)
                    {
                        MyExaminTopic itemTemp;
                        TopicOption optionTemp;
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            itemTemp = new MyExaminTopic();
                            itemTemp.content = ds.Tables[0].Rows[i]["TopicContent"].ToString();
                            itemTemp.exam_id = ds.Tables[0].Rows[i]["TopicID"].ToString();
                            itemTemp.number = (i + 1).ToString();

                            DataRow[] drTemp = ds.Tables[1].Select(" SubjectID='"+ itemTemp.exam_id + "' ");
                            //DataSet dsOption = InterfaceHelper.GetOption(itemTemp.exam_id);
                            //if (dsOption != null && dsOption.Tables[0].Rows.Count > 0)
                            if (drTemp != null && drTemp.Length > 0)
                            {
                                foreach (DataRow dr2 in drTemp)
                                {
                                    optionTemp = new TopicOption();
                                    optionTemp.option_content = dr2["optioncontent"].ToString();
                                    optionTemp.option_id = dr2["optionid"].ToString();
                                    itemTemp.options.Add(optionTemp);
                                }
                            }

                            result.data.list.Add(itemTemp);
                        }
                    }
                    else
                    {
                        result.code = "0";
                        result.desc = "题库没有足够的题目";

                        isFlag = false;
                    }
                }
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-GetMyExaminTopicExList", SerializeHelper.JsonSerializer<MyExaminTopicExInParam>(param), logMsg); 
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<MyExaminTopicExInParam, MyExaminTopicExOutParam>();
            else
                SafeProcessRequest<MyExaminTopicExInParam, MyExaminTopicExOutParam>();
        }
    }

    #region 请求参数
    public class MyExaminTopicExInParam
    {
        /// <summary>
        /// 必填， 	用户ＩＤ
        /// </summary>
        public string user_id
        { get; set; }

        /// <summary>
        /// 必填， 	专题ＩＤ
        /// </summary>
        public string special_id
        { get; set; }  
    }
    #endregion

    #region 结果参数
    public class MyExaminTopicExOutParam
    {
        public MyExaminTopicExOutParam()
        {
            code = "1";
            desc = "操作成功";
            data = new MyExaminTopicList();
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }

        /// <summary>
        /// 考题列表
        /// </summary>
        public MyExaminTopicList data
        { get; set; }
    }
    public class MyExaminTopicList
    {
        public MyExaminTopicList()
        {
            list = new List<MyExaminTopic>();
        }

        public List<MyExaminTopic> list { get; set; }
    }

    /// <summary>
    /// 考题
    /// </summary>
    public class MyExaminTopic
    {
        public MyExaminTopic()
        {
            options = new List<TopicOption>();
        }

        /// <summary>
        ///	题号	
        /// </summary>
        public string number
        { get; set; }

        /// <summary>
        ///	题目ID	
        /// </summary>
        public string exam_id
        { get; set; }

        /// <summary>
        ///	题目内容	
        /// </summary>
        public string content
        { get; set; }

        public List<TopicOption> options { get; set; }
    }

    public class TopicOption
    {
        /// <summary>
        ///	答题选项内容
        /// </summary>
        public string option_content
        { get; set; }

        /// <summary>
        ///	答题选项ID	
        /// </summary>
        public string option_id
        { get; set; }
    }
    #endregion
}